import { http } from '@/utils/http';
import type { LoginResult } from '@/types/auth';
import type { AnyObj } from '@/types/global';
import { useAuthStore } from '@/stores';
import { isLogin, getWxUserId } from '@/utils/auth';
const appId = import.meta.env.VITE_APP_ID;
const clientId = import.meta.env.VITE_CLIENT_ID;
const grantType = 'miniapp';
export const getOpenId = (code : string) => {
	return http({
		method: 'POST',
		url: '/auth/getOpenId',
		data: {
			code,
			appId,
			grantType,
			clientId
		}
	});
}

export const login = (data) => {
	data.appId = appId;
	data.clientId = clientId;
	data.grantType = grantType;
	return http<LoginResult>({
		method: 'POST',
		url: '/auth/login',
		data
	});
}

export const logout = () => {
	return http<LoginResult>({
		method: 'POST',
		url: '/auth/logout',
		data: {
			appId,
			clientId,
			grantType: 'miniapp',
		}
	});
}


export const register = (data) => {
	return http({
		method: 'POST',
		url: '/wx/account/applyAccount',
		data
	});
}

export const updateUserAccount = (data : AnyObj) => {
	return http<LoginResult>({
		method: 'POST',
		url: '/wx/account/updateWxUserAccount',
		data
	});
}


export const getAccountInfo = () => {
	const userId = getWxUserId();
	return http<AnyObj>({
		method: 'GET',
		url: `/wx/account/${userId}`,
	});
}

export const getLawByWxUserId = () => {
	const wxUserId = getWxUserId();
	return http<AnyObj>({
		method: 'GET',
		url: `/wx/lawfirm/manage/${wxUserId}`,
	});
}


export const getBrokerageInfo = (wxUserId : string) => {
	wxUserId = wxUserId ?? getWxUserId();
	return http<AnyObj>({
		method: 'GET',
		url: `/wx/account/brokerageUser/${wxUserId}`,
	});
}

export const getBrokerageUserList = () => {
	const wxUserId = getWxUserId();
	return http<AnyObj[]>({
		method: 'GET',
		url: `/wx/account/listBrokerageUser`,
		data: {
			bindUserId: wxUserId
		}
	});
}



export const saveUnfollow = (followeeId : string) => {
	const authStore = useAuthStore() as any;
	const wxUserId = authStore.profile.userId;
	return http({
		method: 'POST',
		url: `/wx/account/unfollow`,
		data: {
			wxUserId,
			followeeId
		}
	});
}

export const saveFollow = (followeeId : string) => {
	const authStore = useAuthStore() as any;
	const wxUserId = authStore?.profile?.userId ?? '';
	return http({
		method: 'POST',
		url: `/wx/account/follow`,
		data: {
			wxUserId,
			followeeId
		}
	});
}